package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.service.GoodsExtService;
import com.pozu.new_retail.service.GoodsService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;

@Controller
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsExtService goodsExtService;

	@ResponseBody
	@RequestMapping(value = "/api/goods/list", method = RequestMethod.GET)
	public Object getList(HttpServletRequest request) throws Exception{

		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据
		
		String goodsName = request.getParameter("goodsName");//商品名称
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		if(null!=request.getParameter("categoryId")){
			int categoryId = Integer.parseInt(request.getParameter("categoryId"));//大分类id
			map.put("categoryId", categoryId);
		}

		map.put("pageStart", pageStart*pageSize);
		
		if (null != request.getParameter("categoryId")) {
			map.put("categoryId", Integer.parseInt(request.getParameter("categoryId")));//商品分类id
		}
		
		map.put("pageSize", pageSize);
		
		if (null != request.getParameter("saleType")) {//销售类型0预售1在售2促销
			map.put("saleType", Integer.parseInt(request.getParameter("saleType")));
		}
		
		map.put("appId", appId);
		map.put("goodsName",goodsName);
		
		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = goodsService.queryByMap(map);
		
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/goods", method = RequestMethod.GET)
	public Object get(HttpServletRequest request) throws Exception{

		int userId = Integer.parseInt(request.getHeader("userId"));
		
		int goodsId = Integer.parseInt(request.getParameter("goodsId"));//商品id
		
		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = goodsService.queryByGoodsId(goodsId,userId);
		
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/goodsExts", method = RequestMethod.GET)
	public Object getGoodsExts(HttpServletRequest request) throws Exception{

		int goodsId = Integer.parseInt(request.getParameter("goodsId"));//商品id
		
		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		
		
		jr.respData = goodsExtService.queryByGoodsId(goodsId);
		
		return jr;
	}
}
